package com.chat.simplechat.event;

import com.alibaba.fastjson.JSONObject;
import com.chat.simplechat.entity.GiftBean;
import com.chat.simplechat.websocket.SpringWebSocket;
import com.chat.simplechat.websocket.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @author wangyabin
 * @date 2022/10/26 17:43
 */
@Slf4j
@Component
public class GiftEventListener {
    
    @Resource
    private WebSocket webSocket;
    
    @Resource
    private SpringWebSocket springWebSocket;
    
    @EventListener
    public void givingGifts(GiftEvent giftEvent){
        GiftBean giftBean = giftEvent.getGiftBean();
        Random random = new Random();
        String[] str = new String[]{"烟花","跑车","皇冠","凤冠","穿云箭"};
        int i = random.nextInt(str.length);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("fromUserId",giftBean.getFromUserId());
        jsonObject.put("toUserId",giftBean.getToUserId());
        jsonObject.put("contentText",str[i]);
        //webSocket.sendOneMessage(giftBean.getToUserId(),jsonObject.toJSONString());
        springWebSocket.readyMessage(giftBean.getToUserId(),jsonObject.toJSONString());
    }

}
